#ifndef FORMULA_H
#define FORMULA_H

#include "common.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#define ABS(x) (((x) >= 0) ? (x) : (-(x)))
#define ARRAY_LEN(a) (sizeof((a)) / sizeof((a)[0]))

// 用于创建排序函数
#define NEW_CMP(func, type, conv)                                              \
  static int func(const void *a, const void *b) {                              \
    type x = conv(a);                                                          \
    type y = conv(b);                                                          \
    if (x < y)                                                                 \
      return -1;                                                               \
    if (x == y)                                                                \
      return 0;                                                                \
    return 1;                                                                  \
  }

/*
平均值
*/
double avg_double(const double *flist, size_t count);
double avg_uint16_t(const uint16_t *list, size_t count);
double avg_uint32_t(const uint32_t *list, size_t count);
double avg_uint64_t(const uint64_t *list, size_t count);

/*
方差
*/
double var_double(const double *list, size_t count);
double var_i16u(const uint16_t *list, size_t count);
double var_i32u(const uint32_t *list, size_t count);

/*
标准方差
人肉模板生成
*/
double deviation(const double *flist, size_t count);
double deviation_i16u(const uint16_t *ilist, size_t count);
double deviation_i32u(const uint32_t *ilist, size_t count);

/*
掐大去小后，取平均值
 */
double trim_mean(const int32_t *list, size_t count);
/*
对rate和hrv取平均值
@return 成功与否
 */
uint8_t smp_hr_avg(const smp_hr_t *list, size_t count, smp_hr_t *dst);

#ifdef __cplusplus
}
#endif

#endif